﻿@{
    ViewBag.Title = "Brainstorms";
    var url = "";
    var urltopico = "";
    if (Request.IsLocal)
    {
        url = Request.ApplicationPath + "Relatorios/MostraRelatorio.aspx?nomeRelatorio=";
        urltopico = Request.ApplicationPath + "Relatorios/MostraRelatorio.aspx?nomeRelatorio=BrainstormsPorTopico";
    }
    else
    {
        url = Request.ApplicationPath + "/Relatorios/MostraRelatorio.aspx?nomeRelatorio=";
        urltopico = Request.ApplicationPath + "/Relatorios/MostraRelatorio.aspx?nomeRelatorio=BrainstormsPorTopico";
    }
}
<style type="text/css">
    .grid
    {
        min-width: 900px;
        width: 100%;
        margin: auto;
        margin-top: 10px;
        display: none;
    }
    .topico-header
    {
        padding: 4px 5px 4px 10px;
        font-size: 14px;
    }
    .topico
    {
        padding: 0 5px 5px 10px;
        vertical-align: bottom;
    }
    .rel-header, .rel
    {
        text-align: center;
        width: 70px;
        padding: 2px;
    }
    .display-label, .editor-label
    {
        width: 170px;
        text-align: right;
        padding-right: 5px;
    }
    #codigo
    {
        width: 90px;
        text-align: center;
    }
    #btnCliente, #btnFunc, #btnFreela
    {
        height: 21px;
        margin: 0;
        padding: 0 5px 20px 5px;
        line-height: 21px;
        vertical-align: top;
    }
    #opcoes
    {
        width: 700px;
        margin-top: 50px;
        margin-left: 50px;
    }
    #campos
    {
        margin-top: 20px;
    }
    #botao
    {
        clear: both;
        margin-top: 50px;
    }
    .datepicker, #Topico, #procurar
    {
        display: none;
    }
    input[type=button].gerar-rel
    {
        font-size: 12px !important;
        padding: 0 10px 2px 10px !important;
        height: 22px !important;
        line-height: 22px;
        vertical-align: middle;
        margin: 0;
    }
</style>
@if (false)
{
    <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-ui.js" type="text/javascript"></script>
    <script src="../../Scripts/controller.js" type="text/javascript"></script>
}
<script src="@Url.Content("~/Scripts/numbers.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    <div id="opcoes">
        <div>
            <input type="radio" name="tipo" value="codBrain" id="codBrain" checked="checked" />
            <label for="codBrain" style="margin-right: 20px;">
                Código do Brainstorm</label>
            <input type="radio" name="tipo" value="codCamp" id="codCamp" />
            <label for="codCamp" style="margin-right: 20px;">
                Código da Campanha</label>
            <input type="radio" name="tipo" value="datas" id="datas" />
            <label for="datas" style="margin-right: 20px;">
                Por período</label>
            <input type="radio" name="tipo" value="topico" id="topico" />
            <label for="topico" style="margin-right: 20px;">
                Por Tópico</label>
            <input type="radio" name="tipo" value="todos" id="todos" />
            <label for="todos">
                Todos</label>
        </div>
        <div id="campos">
            <div class="editor-label">
                <label for="codigo">
                    Código do Brainstorm:
                </label>
            </div>
            <div class="editor-field">
                @Html.TextBox("Codigo", string.Empty, new { @class = "numero ", maxlength = 7, id = "codigo" })
                @Html.TextBox("DataInicial", DateTime.Today.ToShortDateString(), new { @class = "datepicker", mindate = "01/01/1900", maxdate = "31/12/2015", alt = "date" })
                <span></span>
                @Html.TextBox("DataFinal", DateTime.Today.ToShortDateString(), new { @class = "datepicker", mindate = "01/01/1900", maxdate = "31/12/2015", alt = "date" })
                @Html.TextBox("Topico", string.Empty, new { maxlength = 150, style = "width: 450px;" })
                <span></span>
            </div>
        </div>
        <div id="botao">
            <input type="submit" id="filtrar" value="Gerar Relatório" style="margin-left: 476px;" />
            <input type="button" id="procurar" value="Pesquisar Tópicos" style="margin-left: 454px;" />
        </div>
        <table class="grid">
            <thead>
                <tr>
                    <th class="topico-header">
                        Tópicos
                    </th>
                    <th class="rel-header">
                    </th>
                </tr>
            </thead>
            <tbody>
                
            </tbody>
        </table>
    </div>
}
<script type="text/javascript">
    $(function () {
        $('input[name=tipo]').change(function () {
            var valor = $(this).val();
            switch (valor) {
                default:
                    break;
                case "codBrain":
                    $('label[for=codigo]').html('Código do Brainstorm: ');
                    $('input[type=text]').attr('disabled', false);
                    $('#codigo').val('').show().focus();
                    toogleClassValid($('.datepicker'), $('.datepicker').next(), true);
                    $('.datepicker, #procurar, #Topico, table.grid').hide();
                    $('#filtrar').show();
                    break;
                case "codCamp":
                    $('label[for=codigo]').html('Código da Campanha: ');
                    $('input[type=text]').attr('disabled', false);
                    $('#codigo').val('').show().focus();
                    toogleClassValid($('.datepicker'), $('.datepicker').next(), true);
                    $('.datepicker, #procurar, #Topico, table.grid').hide();
                    $('#procurar').hide();
                    $('#filtrar').show();
                    break;
                case "datas":
                    $('label[for=codigo]').html('Período: ');
                    $('input[type=text]').attr('disabled', false);
                    $('.datepicker').each(function () { $(this).val('@DateTime.Today.ToShortDateString()') }).show();
                    $('#codigo, #procurar, #Topico, table.grid').hide();
                    $('#DataInicial').focus();
                    $('#filtrar').show();
                    break;
                case "topico":
                    $('label[for=codigo]').html('Por Tópico: ');
                    $('input[type=text]').attr('disabled', false);
                    $('#Topico').val('').show().focus();
                    toogleClassValid($('.datepicker'), $('.datepicker').next(), true);
                    $('#codigo, .datepicker, #procurar, #filtrar').hide();
                    $('#procurar').show();
                    break;
                case "todos":
                    $('input[type=text]').attr('disabled', true);
                    toogleClassValid($('.datepicker'), $('.datepicker').next(), true);
                    $('#procurar, table.grid').hide();
                    $('#filtrar').show();
                    break;
            }
        });
        $('.datepicker').mask("99/99/9999");
        $('.datepicker').blur(function () {
            if (!validaData(this)) {
                alert('A data inválida!\n\nPor favor corrija e tente novamente');
            }
        });
        $('#codigo').focus();
    });
    $('form').submit(function (e) {
        e.preventDefault();
        var valor = $('input[name=tipo]:checked').val();
        switch (valor) {
            default:
                break;
            case "codBrain":
                var codigo = $('#codigo').value();
                if (codigo != undefined && codigo != '' && codigo != 0) {
                    var url = '@url' + 'Brainstorms&codigo=' + codigo;
                    window.open(url, '_blank');
                } else {
                    alert('Por favor, digite o código do Brainstorm!');
                }
                break;
            case "codCamp":
                var codigo = $('#codigo').value();
                var url = '@url' + 'BrainstormsPorCampanha&codigo=' + codigo;
                window.open(url, '_blank');
                break;
            case "datas":
                var dataInicio = $('#DataInicial').val();
                var dataFim = $('#DataFinal').val();
                if (validaData('#DataInicial') && validaData('#DataFinal')) {
                    if (validaDataMaior($("#DataInicial"), $("#DataFinal"))) {
                        var url = '@url' + 'Brainstorms&dataInicio=' + dataInicio + '&dataFim=' + dataFim;
                        window.open(url, '_blank');
                    }
                    else {
                        alert('A data inicial não pode ser maior que a dada final!');
                    }
                }
                else {
                    alert('A data inválida!\n\nPor favor corrija e tente novamente');
                }
                break;
            case "todos":
                var url = '@url' + 'Brainstorms';
                window.open(url, '_blank');
                break;
        }
    });
    $('#codigo').focus(function () {
        $(this).select();
    });
    $('#codigo').keydown(function (event) {
        var e = event.keyCode;
        if ((e >= 96 && e <= 105) || (e >= 48 && e <= 57)) {
            return true;
        }
        switch (e) {
            default:
                return false;
                break;
            case 8: // backspace			
                return true;
                break;
            case 9: // tab			
                return true;
                break;
            case 13: // enter
                return true;
                break;
            case 35: // end
                return true;
                break;
            case 36: // home
                return true;
                break;
            case 37: // left arrow
                return true;
                break;
            case 39: // right arrow
                return true;
                break;
            case 46: // delete
                return true;
                break;
        }
    });
    $('#procurar').click(function () {
        showLoading();
        $.get('@Url.Action("PesquisarTopicos", "Relatorio")', { termo: $('#Topico').val() }, function (r) {
            var html = '';
            var counter = 0;
            $.each(r, function (k, v) {
                html += '<tr><td class="topico">' + k + '</td><td class="rel"><input type="button" class="gerar-rel" value="Gerar Relatório" data-id="' + v + '" /></td></tr>';
                counter++
            });
            if (counter == 0) {
                html += '<tr><td class="topico">Não foram encontrados registros</td><td class="rel"><input type="button" class="gerar-rel" value="Gerar Relatório" disabled="disabled" /></td></tr>';
            }
            $('.grid tbody').empty().append(html);
            $('table.grid').show();
            hideLoading();
        });
    });
    $('.gerar-rel').live('click', function () {
        var id = $(this).data('id');
        var url = '@urltopico' + '&codigo=' + id;
        window.open(url, '_blank');
    });
</script>
